package com.springleaf.arena.display
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Rectangle;

	/**
	 * Class containing a grid made using a matrix of cells. The cells are UICell Class.
	 * To use this class properly follow these simple steps:
	 * 
	 * 1. Set the exact rows and columns so you won't have to change it (it will be harder
	 * to redraw else)
	 * 2. Set the cellSheet.
	 * 3. Set the cellDefaultSize.
	 * 4. Set the cellIdleState.
	 * 5. Draw the initial state.
	 * 6. Update.
	 */
	public class UIGrid extends Sprite
	{
		private var grid:Array;
		private var updateableCells:Array; //TO BE USED
		
		private var _rows:int;
		private var _columns:int;
		private var _cellSheet:BitmapData;
		private var _cellDefaultSize:Rectangle;
		private var _cellIdleStates:Array;
		
		public function UIGrid(rows:int, columns:int)
		{
			grid = new Array();
			
			_rows = rows;
			_columns = columns;
		}
		
		/**
		 * You should really make sure that you have the exact number of rows and columns set,
		 * else it will be dificult to redraw everything. Also, you should really load everything
		 * before callint this method. Didn't add them in the constructor so it won't be so big.
		 * 
		 * Everything = sheet, idleStage, defaultSize
		 */
		public function draw():void {
			var cell:UICell;
			
			for (var i:int = 0; i < _rows; i++) {
				grid[i] = new Array();
				for (var j:int = 0; j < _columns; j++) {
					cell = createCell(i, j);
					grid[i][j] = cell;
					addChild(cell);
				}
			}
		}
		
		//TODO add an array where you can store the updateable cells, not all the cells
		public function updateAnimation():void {
			for (var i:int = 0; i < _rows; i++) {
				for (var j:int = 0; j < _columns; j++) {
					(grid[i][j] as UICell).updateAnimation();
				}
			}
		}
		
		private function createCell(i:int, j:int):UICell {
			var cell:UICell = new UICell(_cellIdleStates);
			var columnGap:Number = _cellDefaultSize.height * 0.5;
			var rowGap:Number = _cellDefaultSize.width * 0.5;
			
			cell.sheet = _cellSheet;
			cell.defaultSize = _cellDefaultSize;
			cell.x = -(j * rowGap) + (i * rowGap);
			cell.y = (j * columnGap) + (i * columnGap);
			
			// I was not awake when I wrote this and now I have no idea how it works
			// but it works O_o 
			cell.xPosition = (rowGap * rows) - (i - j) * rowGap;
			cell.yPosition = columnGap + (i - j) * columnGap + j * _cellDefaultSize.height;
			
			cell.draw();
			cell.renderDefaultState();
			return cell;
		}
		
		public function getCellAt(i:int, j:int):UICell {
			if (grid.length < i) {
				return null;
			}
			
			if (grid[i].length < j) {
				return null;
			}
			
			return grid[i][j];
		}
		
		public function get columns():int
		{
			return _columns;
		}
		
		public function set columns(value:int):void
		{
			_columns = value;
		}
		
		public function get rows():int
		{
			return _rows;
		}
		
		public function set rows(value:int):void
		{
			_rows = value;
		}
		
		public function get cellSheet():BitmapData
		{
			return _cellSheet;
		}
		
		public function set cellSheet(value:BitmapData):void
		{
			_cellSheet = value;
		}

		public function get cellDefaultSize():Rectangle
		{
			return _cellDefaultSize;
		}
		
		public function set cellDefaultSize(value:Rectangle):void
		{
			_cellDefaultSize = value;
		}
		

		public function get cellIdleStates():Array
		{
			return _cellIdleStates;
		}
		
		public function set cellIdleStates(value:Array):void
		{
			_cellIdleStates = value;
		}
	}
}